import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { ReportConfigService } from '../report-config.service'

@Component({
    selector: 'report-config-base',
    templateUrl: './report-config-base.component.html',
    styleUrls: ['./report-config-base.component.scss'],
    providers: []
})
export class ReportConfigBaseComponent implements OnInit {
    preventQuickClicks: boolean = false;//防止快速点击
    textAlignTypes: any[] = [{label: '居中对齐', value:'center'},{label: '左对齐', value:'left'},{label: '右对齐', value:'right'}]
    currentReportId: number = 0;
    report: any = {};
    reportForm: FormGroup;
    fields: any[] = [];
    cols: any[] = [
        { header: '字段名称', field: 'fieldName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '显示名称', field: 'fieldDisplayName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '列宽', field: 'fieldWidth', style: { 'text-align': 'center', 'overflow': 'hidden', 'width': '10%' } },
        { header: '列名分组', field: 'fieldGroupName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '前端显示', field: 'visibleFlag', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } },
        { header: '数据类型', field: 'dataTypeName', style: { 'text-align': 'center', 'overflow': 'hidden' } },
        { header: '对齐方式', field: 'textAlign', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } }
    ];

    @Input()
    set reportId(report: number) {
        if (report) {
            this.currentReportId = report;
            this.reportConfigService.loadReport(this.currentReportId).subscribe(data => {
                this.report = data.report;
                this.fields = data.fields;
                this.buildForm();
            });
        }
    }
    get reportId(): number { return this.currentReportId; }

    constructor(
        private fb: FormBuilder,
        private reportConfigService: ReportConfigService,
        private mdlDialogService: MdlDialogService,
        private mdlSnackbarService: MdlSnackbarService) {
    }
    ngOnInit() {
        this.buildForm();
    }

    saveUpdate() {
        this.preventQuickClicks = true;
        let temp = this.fields;
        temp.forEach(e=> e.visibleFlag = (e.visibleFlag?1:0));
        let result = { 'report': this.reportForm.value, 'fields': this.fields }
        this.reportConfigService.updateReport(result).subscribe(data => {
            this.mdlSnackbarService.showToast("保存成功!");
            this.preventQuickClicks = false;
        });
    }

    delete() {
        this.mdlDialogService.confirm("您确定要删除当前选中业务吗？", "取消", "确定").subscribe(() => {
            this.reportConfigService.deleteReport(this.currentReportId).subscribe(data => {
                this.reportConfigService.announceMission(this.currentReportId);
                this.reportConfigService.announceMission({});
                this.mdlSnackbarService.showToast("删除成功!");
            });
        });
    }

    buildForm(): void {
        this.reportForm = this.fb.group({
            'reportId': [this.report.reportId],
            'reportName': [this.report.reportName],
            'groupId': [this.report.groupId],
            'groupName': [this.report.groupName],
            'dsId': [this.report.dsId],
            'dsName': [this.report.dsName],
            'bizId': [this.report.bizId],
            'bizName': [this.report.bizName],
            'reportTypeId': [this.report.reportTypeId],
            'reportTypeName': [this.report.reportTypeName],
            'dataviewName': [this.report.dataviewName],
            'displayOrder': [this.report.displayOrder]
        });
    }


}
